package selef.xiafan.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import selef.xiafan.dao.SysRoleDao;
import selef.xiafan.dao.SysRolePermissionDao;
import selef.xiafan.dao.SysUserRoleDao;
import selef.xiafan.entity.system.SysPermission;
import selef.xiafan.entity.system.SysRole;
import selef.xiafan.entity.system.SysRolePermission;
import selef.xiafan.entity.user.SysUserRole;
import selef.xiafan.entity.user.User;
import selef.xiafan.query.SysRoleQ;
import selef.xiafan.service.ISysPermissionService;
import selef.xiafan.service.ISysRoleService;
import selef.xiafan.util.result.PageResult;

import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class ISysRoleImpServiceImpl implements ISysRoleService {
    @Autowired
    private SysRoleDao sysRoleDao;
    @Autowired
    private SysUserRoleDao sysUserRoleDao;

    @Autowired
    private ISysPermissionService iSysPermissionService;
    @Autowired
    private SysRolePermissionDao sysRolePermissionDao;

    @Override
    public Set<String> getloginRoles(User user) {
        Integer id = user.getId();
        List<Integer> rid = sysUserRoleDao.findAllByUid(id)
                .stream().map(SysUserRole::getRid).collect(Collectors.toList());
        Set<String> roles = sysRoleDao.findAllById(rid)
                .stream().map(SysRole::getName).collect(Collectors.toSet());
        return roles;
    }

    @Override
    public List<SysRole> selectedUserRoles(User user) {
        Integer id = user.getId();
        List<Integer> rid = sysUserRoleDao.findAllByUid(id)
                .stream().map(SysUserRole::getRid).collect(Collectors.toList());
        List<SysRole> sysRoledInDb = sysRoleDao.findAllById(rid);
        return sysRoledInDb;
    }

    @Override
    public PageResult<SysRole> getAll(SysRoleQ sysRoleQ) {
        Integer page = (sysRoleQ.getPage() - 1) * sysRoleQ.getSize();
        Integer size = sysRoleQ.getSize();
        String nameZh = sysRoleQ.getNameZh();


        if (nameZh != "" || nameZh != null) {
            Long total = sysRoleDao.countAllByQuery(nameZh);
            List<SysRole> sysRoleList = sysRoleDao.findAllByQuery(page, size, nameZh);
            List<SysPermission> sysPermissionList;
//            List<SysPermission> sysPermissionMenuList;
//
            Iterator<SysRole> iterator = sysRoleList.iterator();
            while (iterator.hasNext()) {
                SysRole sysRole = iterator.next();
                sysPermissionList = iSysPermissionService.findAllById(sysRole.getId());
//                sysPermissionMenuList = iSysMenuS.getAllMenus(sysRole.getId());
                sysRole.setMenus(sysPermissionList);
//                sysRole.setMenus(sysPermissionMenuList);
            }
            return new PageResult<>(total, sysRoleList);

        }
        Long total = sysRoleDao.countAll();
        if (total == 0L) {
            return new PageResult<SysRole>();
        }
        List<SysRole> sysRoleList = sysRoleDao.findAllNoQuery(page, size);
        List<SysPermission> sysPermissionList;
//        List<SysPermission> sysPermissionMenuList;
//
        Iterator<SysRole> iterator = sysRoleList.iterator();
        while (iterator.hasNext()) {
            SysRole sysRole = iterator.next();
            sysPermissionList = iSysPermissionService.findAllById(sysRole.getId());
//            sysPermissionMenuList = iSysMenuS.getAllMenus(sysRole.getId());
//            sysRole.setPerms(sysPermissionList);
            sysRole.setMenus(sysPermissionList);
        }
        return new PageResult<>(total, sysRoleList);
    }

    @Override
    public SysRole updateRoleStatue(SysRole requestSysRole) {
        SysRole sysRoleInDB = sysRoleDao.findById(requestSysRole.getId()).get();
        sysRoleInDB.setEnable(requestSysRole.getEnable());
        return sysRoleDao.save(sysRoleInDB);
    }

    @Override
    public void addorupdate(SysRole sysRole) {
        System.out.println(sysRole);
        if (sysRole.getId() == null) {
            sysRoleDao.save(sysRole);
        }
        // 删除角色权限中间表信息
        List<Integer> rolePermsId = sysRolePermissionDao.findAllByRid(sysRole.getId())
                .stream().map(SysRolePermission::getId).collect(Collectors.toList());
        if (rolePermsId.size() != 0) {
            Iterator<Integer> deleteIterator = rolePermsId.iterator();
            while (deleteIterator.hasNext()) {
                sysRolePermissionDao.deleteById(deleteIterator.next());
            }
        }

        // 添加中间表信息
        if (sysRole.getPerms() != null) {
            List<Integer> perms = sysRole.getPerms();
            Iterator<Integer> addIterator = perms.iterator();
            while (addIterator.hasNext()) {
                SysRolePermission sysRolePermission = new SysRolePermission();
                sysRolePermission.setPid(addIterator.next());
                sysRolePermission.setRid(sysRole.getId());
                sysRolePermissionDao.save(sysRolePermission);
            }
        }
    }

    @Override
    public List<SysRole> getAll() {
        List<SysRole> sysRoleList = sysRoleDao.findAll();
        List<SysPermission> sysPermissionList;
//        List<SysMenu> sysPermissionMenuList;

        Iterator<SysRole> iterator = sysRoleList.iterator();
        while (iterator.hasNext()) {
            SysRole sysRole = iterator.next();
            sysPermissionList = iSysPermissionService.findAllById(sysRole.getId());
//            sysPermissionMenuList = iSysMenuS.getAllMenus(sysRole.getId());
//            sysRole.setPerms(sysPermissionList);
            sysRole.setMenus(sysPermissionList);
        }
        return sysRoleList;
    }

    @Override
    @Transactional
    public int deleteRole(SysRole role) {
        // 如果是啓用状态，不能删除
        if (role.getEnable()) {
            return 0;
        }
        sysRoleDao.deleteById(role.getId());
        // 删除中间表
        sysRolePermissionDao.deleteAllByRid(role.getId());
        return 1;
    }
}
